#include "PrecompiledHeader_Engineer.h"
#include "WMobs.h"

/*--------------------------------------------------------------------------*/

WMobs::WMobs()
{
	this->m_maxMobCount = 30;
}

/*--------------------------------------------------------------------------*/

WMobs::~WMobs()
{

}

/*--------------------------------------------------------------------------*/

void WMobs::SpawnNewMob()
{
	float random = rand() % 1000 / 1000.0f * vl::Pi * 2.0f;
	glm::vec3 pos = glm::vec3(glm::sin(random), glm::cos(random), 0.0f) * 60.0f + WPlayer::getInstance()->GetPlayerPosition();

	this->m_mobs.push_back(new IMob(pos, 100.0f, 5.5f));
}

/*--------------------------------------------------------------------------*/

unsigned int WMobs::GetMobCount() const
{
	return this->m_mobs.size();
}

/*--------------------------------------------------------------------------*/

unsigned int WMobs::GetMaxMobCount() const
{
	return this->m_maxMobCount;
}

/*--------------------------------------------------------------------------*/

void WMobs::SetMaxMobCount(unsigned int i)
{
	this->m_maxMobCount = i;
}

/*--------------------------------------------------------------------------*/

void WMobs::OnLevelLoad(const std::string& level)
{
	this->m_zombie = new Sound*[4];

	CResources::getInstance()->Get("zombie1", &(this->m_zombie[0]));
	CResources::getInstance()->Get("zombie2", &(this->m_zombie[1]));
	CResources::getInstance()->Get("zombie3", &(this->m_zombie[2]));
	CResources::getInstance()->Get("zombie4", &(this->m_zombie[3]));
}

/*--------------------------------------------------------------------------*/

void WMobs::OnLevelUnload()
{
	for (std::list<IMob*>::iterator i = this->m_mobs.begin(); i != this->m_mobs.end();)
	{
		//CTree::getInstance()->DeleteObject(*i);
	}
}

/*--------------------------------------------------------------------------*/

void WMobs::OnLevelCalc()
{
	glm::vec3 playerPos = WPlayer::getInstance()->GetPlayerPosition();

	for (std::list<IMob*>::iterator i = this->m_mobs.begin(); i != this->m_mobs.end();)
	{
		if ((*i)->GetHealth() <= 0.0f)
		{
			i = this->m_mobs.erase(i);
			//CTree::getInstance()->DeleteObject(*i);
		}
		else
		{
			glm::vec3 direction = playerPos - (*i)->GetPosition() + glm::vec3(glm::sin(rand() % 100 * 0.01f * vl::Pi * 2.0f), glm::cos(rand() % 100 * 0.01f * vl::Pi * 2.0f), 0.0f);
			direction.z = 0.0f;
			direction = glm::normalize(direction);
			direction *= (*i)->GetSpeed() * CSdl::getInstance()->GetDeltaTime();

			glm::vec3 npos = (*i)->GetPosition() + direction;

			WTerrain::getInstance()->GetHeight(npos);

			(*i)->SetPosition(npos);

			if (glm::distance(npos, playerPos) < 2.0f)
			{
				WPlayer::getInstance()->SetPlayerHealth(WPlayer::getInstance()->GetPlayerHealth() - 0.0001f);
			}

			++i;


			if (rand() % 1000 == 999)
			{
				CMixer::getInstance()->PlayNow(this->m_zombie[rand() % 4]);
			}

		}
	}

	int maxMobs = this->m_maxMobCount * max(0.0f, glm::cos((WWeather::getInstance()->GetTime()) * glm::pi<float>() / 720.0f));

	if (this->GetMobCount() < maxMobs && rand() % 100 > 90)
	{
		this->SpawnNewMob();
	}
}

/*--------------------------------------------------------------------------*/

std::list<IMob*>& WMobs::GetMobs()
{
	return this->m_mobs;
}

/*--------------------------------------------------------------------------*/

void WMobs::KillAll()
{
	for (std::list<IMob*>::iterator i = this->m_mobs.begin(); i != this->m_mobs.end(); ++i)
	{
		(*i)->Kill();
	}
}

/*--------------------------------------------------------------------------*/